﻿Imports System.IO

Public Class eventlogs

    Private Sub DeleteLogs()
        Dim Date_ As DateTime = DateTime.Now
        Dim format As String = "yyyyMMdd"
        Dim LogPath As String
        LogPath = My.Application.Info.DirectoryPath & "\EVENTS_" & Date_.ToString(format) & ".txt"
        Try
forcedel:
            If File.Exists(LogPath) Then
                File.Delete(LogPath)
                ListView1.Items.Clear()
            End If
        Catch ex As Exception
            GoTo forcedel
        End Try
    End Sub
    Function CheckDuplicate(key As String) As Boolean
        Try
            For Each item As ListViewItem In ListView1.Items
                Dim myKey As String = item.SubItems(0).Text
                ' MsgBox(myKey & "VS" & key)
                If myKey = key Then
                    Return True
                End If
            Next
            Return False
        Catch ex As Exception
            Return True
        End Try
        Return True
    End Function

    Private Sub eventlogs_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim Date_ As DateTime = DateTime.Now
        Dim format As String = "yyyyMMdd"
        Console.WriteLine(Date_.ToString(format))
        Dim LogPath As String
        LogPath = My.Application.Info.DirectoryPath & "\EVENTS_" & Date_.ToString(format) & ".txt"
        Try
            If System.IO.File.Exists(LogPath) = True Then
                Dim objReader As New System.IO.StreamReader(LogPath)
                Do While objReader.Peek() <> -1
                    Dim spliter As String()
                    spliter = objReader.ReadLine().Split("|")
                    With ListView1.Items.Add(spliter(0))
                        .SubItems.AddRange(New String() {spliter(1)})
                    End With
                Loop
                objReader.Close()
            End If

        Catch ex As Exception
        End Try
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim t As New Threading.Thread(AddressOf DeleteLogs)
        t.Start()
    End Sub

End Class
